package com.example.hyclipboard;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import java.text.MessageFormat;
import java.util.ArrayList;

public class DatabaseDao {
	public static class MemoData {
		public String cid;
		public String updateTime;
		public String deviceName;
		public int read;
		public String content;
	}

	public static final int STATUS_SUCCESS = 0;
	public static final int STATUS_FAILED = 1;

	private final String TAG = "DatabaseDao";
	private final String DB_NAME = "Content.db";
	private final String TABLE_NAME_CONTENT = "Content";
	private final String COLUMN_CID = "CID";
	private final String COLUMN_CONTENT = "Content";
	private final String COLUMN_UID = "UID";
	private final String COLUMN_ADD_TIME = "AddTime";
	private final String COLUMN_ALREADY_READ = "AlreadyRead";
	private final String COLUMN_FROM_DEVICE = "FromDevice";
	private SQLiteDatabase sqLiteDatabase;

	public DatabaseDao(Context context) {
		DatabaseHelper helper = new DatabaseHelper(context, DB_NAME, 5);
		this.sqLiteDatabase = helper.getWritableDatabase();
	}

	public int insertContent(MemoData memoData) {
		Object[] arr = new Object[]{
				TABLE_NAME_CONTENT, COLUMN_CID, COLUMN_CONTENT, COLUMN_ADD_TIME, COLUMN_ALREADY_READ, COLUMN_FROM_DEVICE,
									memoData.cid, memoData.content, memoData.updateTime, memoData.read, memoData.deviceName
		};
		String sql = MessageFormat.format("insert into {0} ({1}, {2}, {3}, {4}, {5}) values (''{6}'', ''{7}'', ''{8}'', {9}, ''{10}'')", arr);
		try {
			sqLiteDatabase.execSQL(sql);
			return STATUS_SUCCESS;
		} catch (SQLException e) {
			e.printStackTrace();
			Log.e(TAG, e.getClass().getName());
			Log.e(TAG, sql);
			return STATUS_FAILED;
		}
	}

	@SuppressLint("Range")
	public ArrayList<MemoData> queryContent() {
		ArrayList<MemoData> memoDataList = new ArrayList<>();
		String sql = MessageFormat.format("select * from {0} order by {1} desc", TABLE_NAME_CONTENT, COLUMN_ADD_TIME);
		try (Cursor cursor = sqLiteDatabase.rawQuery(sql, null)) {
			Log.e(TAG, sql);
			for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
				MemoData memoData = new MemoData();
				memoData.cid = cursor.getString(cursor.getColumnIndex(COLUMN_CID));
				memoData.updateTime = cursor.getString(cursor.getColumnIndex(COLUMN_ADD_TIME));
				memoData.read = cursor.getInt(cursor.getColumnIndex(COLUMN_ALREADY_READ));
				memoData.deviceName = cursor.getString(cursor.getColumnIndex(COLUMN_FROM_DEVICE));
				memoData.content = cursor.getString(cursor.getColumnIndex(COLUMN_CONTENT));
				memoDataList.add(memoData);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			Log.e(TAG, e.getClass().getName());
			Log.e(TAG, sql);
		}
		return memoDataList;
	}

	public int deleteAllContent() {
		String sql = MessageFormat.format("TRUNCATE TABLE {0};", TABLE_NAME_CONTENT);
		try {
			sqLiteDatabase.execSQL(sql);
			return STATUS_SUCCESS;
		} catch (SQLException e) {
			e.printStackTrace();
			Log.e(TAG, e.getClass().getName());
			Log.e(TAG, sql);
			return STATUS_FAILED;
		}
	}

	class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(@Nullable Context context, @Nullable String name, int version) {
			super(context, name, null, version);
		}

		@Override
		public void onCreate(SQLiteDatabase sqLiteDatabase) {
			createTable(sqLiteDatabase);
		}

		@Override
		public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
			createTable(sqLiteDatabase);
		}

		private void createTable(SQLiteDatabase sqLiteDatabase) {
			Object[] arr = new String[]{TABLE_NAME_CONTENT, COLUMN_CID, COLUMN_CONTENT, COLUMN_ADD_TIME, COLUMN_ALREADY_READ, COLUMN_FROM_DEVICE};
			String sql = MessageFormat.format("create table {0} (\n" +
					"    {1}         varchar(36) not null\n" +
					"        primary key,\n" +
					"    {2}     text        not null,\n" +
					"    {3}     timestamp   not null,\n" +
					"    {4} tinyint(1)  not null,\n" +
					"    {5}  varchar(20) null" +
					");", arr);
			try {
				Log.e(TAG, sql);
				sqLiteDatabase.execSQL(sql);
			} catch (SQLException e) {
				e.printStackTrace();
				Log.e(TAG, e.getClass().getName());
				Log.e(TAG, sql);
			}
		}
	}
}
